<template>
  <div>

      <!-- 搜素、添加表单 -->
      <el-card>
        <el-form :inline="true" class="demo-form-inline">
          <i class="el-icon-search"></i>
          <span>筛选搜索</span>
          <el-form-item label="" style="margin-top: 30px;">
            <el-input
              placeholder="支付方式"
              v-model="input"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <!-- <el-button type="primary">查询</el-button> -->
            <el-button
              type="primary"
              @click="handleAdd"
              style="margin-top: 30px;"
            >
              添加
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
  
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <!-- 支付方式 -->
    <el-table :data="tables" style="width: 100%;" border>
      <el-table-column
        label="Id"
        width="180"
        prop="id"
        type="index"
        align="center"
      >
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px;">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付方式"
        width="180"
        prop="ways"
        align="center"
      >
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.ways }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <!-- scope.row  代表一行的数据 scope.row.msg_state 代表状态的值 -->
          <!-- {{scope.row}} -->
          <el-switch
            v-model="scope.row.isActived"
            @change="onChange(scope.$index, scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column
        label="备注"
        prop="remarks"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 模态框，用于用户的增加和修改 -->
    <el-dialog title="支付" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="支付方式" :label-width="formLabelWidth">
          <el-input
            v-model="formData.ways"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: center;"
      >
        <el-button
          type="primary"
          @click="handleSave"
          style="margin-right: 30px;"
        >
          确 定
        </el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 分页组件 -->
    <div class="block">
      <el-pagination
        @size-change="handlePageSizeChange"
        @current-change="handlePageIndexChange"
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.rowsTotal"
        background
      ></el-pagination>
    </div>
  </div>
</template>

<script>

import {
  getList,
  modPay,
  addPay,
  vimState,
  deletePay
 
} from '../../api/pay'
// import ImgUpload from './ImgUpload'

export default {
  data() {
    return {
      input: '',
      tableData: [],
      pager: {
        pageIndex: 1,
        pageSize: 5,
        rowsTotal: 30,
      },
      dialogFormVisible: false,
      formData: {
        id: 0,
        ways:'', 
        remarks: '',
      },
      formLabelWidth: '120px',
      currentIndex: 0,
    }
  },

  //挂载
  mounted() {
    this.fetchData(this.pager)
    setTimeout(() => {
      // console.log(this.tableData)
    }, 1000)
  },
  //搜索
  computed: {
    tables() {
      const input = this.input
      if (input) {
        console.log('input输入的搜索内容：' + this.input)
        return this.tableData.filter((data) => {
          return data.ways.includes(this.input)
        })
      }

      return this.tableData
    },
  },
  methods: {
    //状态
    onChange(index, row) {
      console.log(index, row)
      this.formData.isActived = row.isActived
      vimState(row.id, this.formData).then((data) => {
        console.log(data)
        if (data.data.isActived == false) {
          console.log(data.msg)
          this.$message.error(data.msg)
        } else {
          console.log(data.msg)
          this.$message.success(data.msg)
        }
      })
    },
    //查询
    handelSelect() {
      this.dialogFormVisible = false

      getList().then((data) => {
        console.log(data)
        this.tableData = data.data.data
      })
    },
    // 用户添加
    handleAdd() {
      this.dialogFormVisible = true
      this.formData.id = 0
      this.formData.ways = ''
      this.formData.remarks=''
    
    },
    // 用户修改
    handleEdit(index, row) {
      this.dialogFormVisible = true
      this.formData.id = row.id
      this.formData.ways = row.ways
      this.formData.remarks=row.remarks
      // 编辑时获得当前表数组中的下标
      this.currentIndex = index
      console.log(index, row)
    },
    // 删除用户
    handleDelete(index, row) {
      //删除指定id用户
      this.tableData.splice(index, 1)
      //相应删掉id的数据
      deletePay(row.id)
      //相应的表数据要减掉一条数据
      this.pager.rowsTotal = this.pager.rowsTotal - 1
      this.$message.success('删除成功')
      console.log(index, row)
      console.log(index, row)
    },

 
    handleSave() {
      this.dialogFormVisible = false
      console.log(this.formData)
      // 如果表单中的id为0，则认为是添加用户，反之则为修改用户
      if (this.formData.id === 0) {
        addPay(this.formData)
          .then((data) => {
            console.log(data)
            //将新添加的数据push到现有表数据的最后面
            if (data.code == 400) {
              this.$message.error(data.msg)
            } else {
              this.tableData.push(data.data)
              this.$message.success(data.msg)
              this.fetchData(this.pager)
            }
          })
          .catch((err) => {
            console.log(err)
          })
      } else {
        modPay(this.formData.id, this.formData)
          .then((data) => {
            console.log(data)
            //修改完重新拉取数据
            this.tableData.splice(this.currentIndex, 1, data.data)
            // this.fetchData(this.pager)
            this.$message.success('编辑成功')
          })
          .catch((err) => {
            console.log(err)
          })
      }
    },
    handleCancel() {
      this.dialogFormVisible = false
    },
    // 页面页码改变时，重新拉取数据
    handlePageIndexChange(val) {
      console.log(val)
      this.pager.pageIndex = val > 0 ? val : 1
      this.fetchData(this.pager)
    },
    // 页面大小改变时，重新拉取数据
    handlePageSizeChange(val) {
      console.log(val)
      this.pager.pageSize = val
      this.fetchData(this.pager)
    },

    // 拉取后台数据（带分页）
    fetchData(pager) {
      getList(pager).then((data) => {
        console.log(data)

        this.tableData = data.data.data
        console.log(this.tableData)
        this.pager = data.data.pager
      })
    },
  },
}
</script>

<style></style>
